package com.controller;


import com.api.CommonResult;

import com.entity.FUser;
import com.entity.table.FUserTableDef;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.FUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表 控制层。
 * @author admin
 * @since 2024-05-05
 */
@Controller
@RequestMapping("/admin/user")
public class FUserController {

    @Autowired
    private FUserService fUserService;

    //按用户名模糊查询
    @GetMapping("/userList")
    public String userList(ModelMap model,
                           @RequestParam(value = "userName", defaultValue = "") String userName,
                           @RequestParam(value = "p", defaultValue = "1") int p,
                           @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(FUserTableDef.F_USER.USER_NAME.like(userName));
        Page page = new Page();
        page.setPageSize(pageSize);//设置每页记录数
        page.setPageNumber(p);//设置页码
        Page pageInfo = fUserService.page(page, queryWrapper);
        model.addAttribute("page", pageInfo);
        model.addAttribute("total", pageInfo.getTotalRow());
        return "back/user-list";
    }

    //添加会员页面代码
    @GetMapping("/addView")
    public String addUserView() {
        return "back/user-add";
    }

    /**
     * 添加用户表代码。
     * @param fUser 用户表
     * @return {@code true} 添加成功, {@code false} 添加失败
     */
    @PostMapping("/save")
    @ResponseBody
    public CommonResult save(FUser fUser) {
        try {
            boolean r = fUserService.save(fUser);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = fUser.getUserName() + "已存在";
            return CommonResult.failed(msg);
        }
    }

    //添加修改管理员页面代码
    @GetMapping("/updateView")
    public String updateView(int id, ModelMap model) {
        FUser fUser1 = fUserService.getById(id);
        model.addAttribute("user", fUser1);
        return "back/user-edit";
    }

    /**
     * 修改根据主键更新用户表代码。
     * @param fUser 用户表
     * @return {@code true} 更新成功, {@code false} 更新失败
     */
    @PostMapping("/update")
    @ResponseBody
    public CommonResult update(FUser fUser) {
        try {
            boolean r = fUserService.updateById(fUser);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = fUser.getUserName() + "已存在";
            return CommonResult.failed(msg);
        }
    }

    //添加删除用户代码
    @PostMapping("/del")
    @ResponseBody
    public CommonResult del(Integer id) {
        try {
            boolean r = fUserService.removeById(id);
            return CommonResult.success(r);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
    }
}
